package com.zbkj.service.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.zbkj.common.request.PageParamRequest;
import com.github.pagehelper.PageHelper;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zbkj.common.exception.AppException;
import com.zbkj.common.response.BrandAgentRegionPageDataResponse;
import com.zbkj.common.request.BrandAgentRegionSearchRequest;
import com.zbkj.common.request.BrandAgentRegionSaveRequest;

import com.zbkj.common.result.CommonResultCode;
import com.zbkj.common.model.agent.BrandAgentRegion;
import com.zbkj.service.dao.BrandAgentRegionDao;
import com.zbkj.service.service.BrandAgentRegionService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
* @author dazongzi
* @description BrandAgentRegionServiceImpl 接口实现
* @date 2025-07-01
*/
@Service
public class BrandAgentRegionServiceImpl extends ServiceImpl<BrandAgentRegionDao, BrandAgentRegion> implements BrandAgentRegionService {

    @Resource
    private BrandAgentRegionDao dao;

    @Override
    public Boolean create(BrandAgentRegionSaveRequest brandAgentRegionSaveRequest) {
        BrandAgentRegion brandAgentRegion = new BrandAgentRegion();
        BeanUtils.copyProperties(brandAgentRegionSaveRequest, brandAgentRegion, "id");
        boolean save = save(brandAgentRegion);
        if (!save) {
            throw new AppException(CommonResultCode.VALIDATE_FAILED, "添加失败");
        }
        return save;
    }

    /**
     * 分页查询品牌商代理设置表
     *
     * @param request          请求参数
     * @param pageParamRequest 分页类参数
     * @return List<BrandAgentRegion>
     * @author dazongzi
     * @since 2025-07-01
     */
    @Override
    public List<BrandAgentRegionPageDataResponse> getList(BrandAgentRegionSearchRequest request, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());

        //带 BrandAgentRegion 类的多条件查询
        LambdaQueryWrapper<BrandAgentRegion> lambdaQueryWrapper = Wrappers.lambdaQuery();

        lambdaQueryWrapper.orderByDesc(BrandAgentRegion::getCreateTime);
        List<BrandAgentRegion> brandAgentRegions = dao.selectList(lambdaQueryWrapper);
        // 添加活动状态
        List<BrandAgentRegionPageDataResponse> brandAgentRegionResponses = new ArrayList<>();

        brandAgentRegions.stream().forEach(x->{
            BrandAgentRegionPageDataResponse brandAgentRegionResponse = new BrandAgentRegionPageDataResponse();
            BeanUtils.copyProperties(x, brandAgentRegionResponse);
            brandAgentRegionResponses.add(brandAgentRegionResponse);
        });
        return brandAgentRegionResponses;
    }

    @Override
    public List<BrandAgentRegion> getListByBrandAgentId(Integer brandAgentId) {
        LambdaQueryWrapper<BrandAgentRegion> lqw = Wrappers.lambdaQuery();
        lqw.eq(BrandAgentRegion::getIsDel, 0);
        lqw.eq(BrandAgentRegion::getBrandAgentId, brandAgentId);
        lqw.orderByDesc(BrandAgentRegion::getCreateTime);
        return this.list(lqw);
    }

    @Override
    public void deleteByConfigId(List<Integer> auditIdList) {
        dao.delete(new LambdaQueryWrapper<BrandAgentRegion>().in(BrandAgentRegion::getBrandAgentId, auditIdList));
    }

    @Override
    public List<BrandAgentRegion> getList(Integer brandId, String districtId, Integer level) {
        LambdaQueryWrapper<BrandAgentRegion> lqw = Wrappers.lambdaQuery();
        lqw.eq(BrandAgentRegion::getIsDel, 0);
        lqw.eq(BrandAgentRegion::getRegionId, districtId);
        lqw.eq(BrandAgentRegion::getRegionType, level);
        lqw.orderByAsc(BrandAgentRegion::getCreateTime);
        return list(lqw);
    }

    @Override
    public List<BrandAgentRegion> getNextLevelNameList(Integer brandId, List<Integer> auditIdList, int level) {
        LambdaQueryWrapper<BrandAgentRegion> lqw = Wrappers.lambdaQuery();
        lqw.select(BrandAgentRegion::getRegionName);
        lqw.eq(BrandAgentRegion::getIsDel, 0);
        lqw.eq(BrandAgentRegion::getBrandAgentId, auditIdList);
        lqw.eq(BrandAgentRegion::getRegionType, level);
        lqw.orderByAsc(BrandAgentRegion::getCreateTime);
        return list(lqw);
    }

    @Override
    public BrandAgentRegion getCommunity(Integer id) {
        LambdaQueryWrapper<BrandAgentRegion> lqw = Wrappers.lambdaQuery();
        lqw.select(BrandAgentRegion::getCommunity, BrandAgentRegion::getId);
        lqw.eq(BrandAgentRegion::getIsDel, 0);
        lqw.eq(BrandAgentRegion::getBrandAgentId, id);
        lqw.orderByAsc(BrandAgentRegion::getCreateTime);
        return getOne(lqw, false);

    }


}

